#include "mainwidget.h"
#include "ui_mainwidget.h"

MainWidget::MainWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MainWidget)
{
    ui->setupUi(this);

    // 打开数据库连接
    SERIALPORTCTRL_INSTANCE->openDbConnect();
    // upd接收
    connect( &m_thread , SIGNAL(newDataRcved(QByteArray)) , this , SLOT(newDetectNodeDataRcvedSlot(QByteArray) ) );
    m_thread.start();


    // 打开串口
    SERIALPORTCONFIG_INSTANCE->getSerialPortInfo(m_vecSerialInfo);

    strName = m_vecSerialInfo[3].name;
}

MainWidget::~MainWidget()
{
    delete ui;
}

// 疲劳驾驶
void MainWidget::on_fatigueBtn_clicked()
{
    Dialog_Sfatigue *m_sfatigue = new Dialog_Sfatigue();
    m_sfatigue->setAttribute(Qt::WA_DeleteOnClose);
    m_sfatigue->show();
}

// 车辆状态
void MainWidget::on_carStateBtn_clicked()
{
    Dialog_Svehicle *m_svehicle = new Dialog_Svehicle();
    m_svehicle->setAttribute(Qt::WA_DeleteOnClose);
    m_svehicle->show();
}

// 货物位移
void MainWidget::on_cargoBtn_clicked()
{
    Dialog_Sdrift *m_sdrift = new Dialog_Sdrift();
    m_sdrift->setAttribute(Qt::WA_DeleteOnClose);
    m_sdrift->show();
}

// 北斗发送
void MainWidget::on_compassSendBtn_clicked()
{
    Dialog_Compass *m_compass = new Dialog_Compass();
    m_compass->setAttribute(Qt::WA_DeleteOnClose);
    m_compass->show();
}

// 北斗接收
void MainWidget::on_compassReciveBtn_clicked()
{

    // 打开串口
    if(SERIALPORTCTRL_INSTANCE->isOpened(strName)){
        if(ui->compassReciveBtn->text() == "北斗接收"){
            ui->compassReciveBtn->setText("北斗接收中");
        }else{
            SERIALPORTCTRL_INSTANCE->close_port(strName);
            ui->compassReciveBtn->setText("北斗接收");
        }
    }else{
        SERIALPORTCTRL_INSTANCE->open_port(strName);
        ui->compassReciveBtn->setText("北斗接收中");
    }

}

// 清空接收
void MainWidget::on_clearReciveBtn_clicked()
{
    ui->toReceiveData->clear();
}

// 数据处理后更新页面
void MainWidget::newDetectNodeDataRcvedSlot(QByteArray m_data)
{
    QString receiveData = QString::fromLocal8Bit(m_data);

    // 页面显示
    ui->toReceiveData->setPlainText(receiveData);
}

